<?php

namespace App\Clients;

use App\Models\JLAccount;
use App\Models\JLApp;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;


class VivoClient extends BaseClient
{

    /**
     * 获取 Access_token
     * @param $auth_code String 授权码
     * @param $appModel  JLApp App配置
     * @return array
     */
    public static function getAccessToken($auth_code, $appModel)
    {
        $result = self::get("https://marketing-api.vivo.com.cn/openapi/v1/oauth2/token", [
            "query"=>[
                "client_id"     => $appModel['app_id'],
                "client_secret"     => $appModel['app_secret'],
                "grant_type" => "code",
                "code"  => $auth_code,
            ]
        ]);
        $body   = $result->getBody()->getContents();

        return json_decode($body, true);
    }

    public static function getAccountInfo($token ) {
        $result = self::get("https://marketing-api.vivo.com.cn/openapi/v1/account/fetch", [
            "query" => [
                "access_token" => $token,
                "timestamp" => time()*1000,
                "nonce" => generateRandomString(),
//                "advertiser_id" => $id
            ]
        ]);
        $body   = $result->getBody()->getContents();

        return json_decode($body, true);
    }
}
